package com.gluck.cases.boostrap.filter;
import com.gluck.cases.common.utils.constants.BizSerConstant;
import com.gluck.cases.common.utils.enums.BizSerErrorEnum;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import java.io.IOException;

/**
 * @ClassName ExceptionFilter
 * @Desc TODO
 * @Author I am Gluck
 * @Date 2022/5/18 - 5:06 下午
 **/
@Component
public class ExceptionFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        try {
            chain.doFilter(request, response);
        } catch (Exception e) {
            // 异常捕获，发送到error controller
            request.setAttribute(BizSerConstant.ERROR_MSG, e);
            request.setAttribute(BizSerConstant.ERROR_CODE, BizSerErrorEnum.FILTER_ERROR.getGluckStockErrorCode());
            //将异常分发到/error/exthrow控制器
            request.getRequestDispatcher("/throws").forward(request, response);
        }
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {
    }
}
